from sqlalchemy.orm import sessionmaker

from api.models.api_models import FaceVideoGeneration


class FaceVideoGenerationDAO:
    def __init__(self, engine):
        self.engine = engine
        self.Session = sessionmaker(bind=self.engine, expire_on_commit=False)

    def create(self, face_picture_id, video_id, ppt_file_id, generate_time):
        session = self.Session()
        face_video_generation = FaceVideoGeneration(face_picture_id=face_picture_id, video_id=video_id, ppt_file_id=ppt_file_id, generate_time=generate_time)
        session.add(face_video_generation)
        session.commit()
        session.close()
        return face_video_generation

    def read(self, face_picture_id, video_id, ppt_file_id):
        session = self.Session()
        face_video_generation = session.query(FaceVideoGeneration).filter_by(face_picture_id=face_picture_id, video_id=video_id, ppt_file_id=ppt_file_id).first()
        session.close()
        return face_video_generation

    def update(self, face_picture_id, video_id, ppt_file_id, generate_time=None):
        session = self.Session()
        face_video_generation = session.query(FaceVideoGeneration).filter_by(face_picture_id=face_picture_id, video_id=video_id, ppt_file_id=ppt_file_id).first()
        if generate_time is not None:
            face_video_generation.generate_time = generate_time
        session.commit()
        session.close()

    def delete(self, face_picture_id, video_id, ppt_file_id):
        session = self.Session()
        face_video_generation = session.query(FaceVideoGeneration).filter_by(face_picture_id=face_picture_id,
                                                                             video_id=video_id,
                                                                             ppt_file_id=ppt_file_id).first()
        session.delete(face_video_generation)
        session.commit()
        session.close()